//
//  WmcBubbleView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/5/11.
//

import UIKit
import YPBedrock

// MARK: - 扩展WmcBubbleView + WmcVMProtocol

extension WmcBubbleView: WmcVMProtocol {
    typealias VMType = WmcBubbleVM
}

// MARK: - [WmcBubbleView]

open class WmcBubbleView: WmcBaseView {
    lazy var imageView: UIImageView = {
        let imageView = UIImageView()
        return imageView
    }()

    lazy var label: UILabel = {
        let label = UILabel()
        return label
    }()

    lazy var imageTextView: WmcBubbleImageTextView = {
        let view = WmcBubbleImageTextView()
        return view
    }()

    lazy var contentView: UIView = {
        let view = UIView()
        return view
    }()

    lazy var arrowView: WmcBubbleArrowView = {
        let view = WmcBubbleArrowView()
        return view
    }()
    
    lazy var closeBtn: UIButton = {
        let btn = UIButton()
        btn.yp.addEvent { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.dismiss()
            weakSelf.closeClosure?()
        }
        return btn
    }()
    
    var closeClosure: (() -> Void)?

    func setViewModel(_ vm: VMType) {
        viewModel = vm
        _makeUI()
    }
    
    func _makeUI() {
        backgroundColor = .clear
        layer.shadowColor = viewModel.bubbleShadowColor.cgColor
        layer.shadowOpacity = viewModel.bubbleShadowOpacity.yp.float
        layer.shadowOffset = viewModel.bubbleShadowOffset
        layer.shadowRadius = viewModel.bubbleShadowRadius
        
        contentView.layer.cornerRadius = viewModel.contentCornerRadius
        contentView.backgroundColor = viewModel.contentBackgroundColor
        addSubview(contentView)
        contentView.snp.makeConstraints { make in
            switch viewModel.showDirection {
            case .top:
                make.top.left.right.equalToSuperview()
                make.bottom.equalTo(-viewModel.arrowHeight)
            case .left:
                make.top.bottom.left.equalToSuperview()
                make.right.equalTo(-viewModel.arrowHeight)
            case .bottom:
                make.bottom.left.right.equalToSuperview()
                make.top.equalTo(viewModel.arrowHeight)
            case .right:
                make.top.bottom.right.equalToSuperview()
                make.left.equalTo(viewModel.arrowHeight)
            }
        }

        // 展示的视图
        var showView: UIView?

        // 处理内容类型
        switch viewModel.contentStyle {
        case let .text(text, config: config):
            config.settingLabel(label)
            label.text = text
            showView = label

        case let .image(image, config: config):
            config.settingImageView(imageView)
            imageView.image = image
            showView = imageView

        case let .imageText(image: image, string: string, style: style, config: config):
            imageTextView.setImageTextStyle(style, config: config)
            imageTextView.imageView.image = image
            imageTextView.label.text = string
            showView = imageTextView

        case let .attributedText(attributedText, config: config):
            config.settingLabel(label)
            label.attributedText = attributedText
            showView = label

        case let .imageAttributedText(image: image, attributedText: attributedText, style: style, config: config):
            imageTextView.imageView.image = image
            imageTextView.setImageTextStyle(style, config: config)
            imageTextView.label.attributedText = attributedText
            showView = imageTextView

        case let .custom(view: view):
            contentView.addSubview(view)
            showView = view
        }
        guard let showView = showView else { return }
        // 布局内容
        contentView.addSubview(showView)
        if viewModel.useCloseBtn {
            closeBtn.setImage("close".wmc_image.yp.set(tintColor: viewModel.closeBtnTintColor), for: .normal)
            contentView.addSubview(closeBtn)
            switch viewModel.closeBtnDirection {
            case .top:
                closeBtn.snp.makeConstraints { make in
                    make.top.equalTo(viewModel.contentInsetEdge.top)
                    make.left.equalTo(viewModel.contentInsetEdge.left)
                    make.right.equalTo(-viewModel.contentInsetEdge.right)
                    make.height.equalTo(closeBtn.snp.width)
                }
                showView.snp.makeConstraints { make in
                    make.top.equalTo(closeBtn.snp.bottom).offset(viewModel.closeBtnContentViewSpacing)
                    make.left.equalTo(viewModel.contentInsetEdge.left)
                    make.bottom.equalTo(-viewModel.contentInsetEdge.bottom)
                    make.right.equalTo(-viewModel.contentInsetEdge.right)
                }
            case .left:
                closeBtn.snp.makeConstraints { make in
                    make.top.equalTo(viewModel.contentInsetEdge.top)
                    make.left.equalTo(viewModel.contentInsetEdge.left)
                    make.bottom.equalTo(-viewModel.contentInsetEdge.bottom)
                    make.width.equalTo(closeBtn.snp.height)
                }
                showView.snp.makeConstraints { make in
                    make.top.equalTo(viewModel.contentInsetEdge.top)
                    make.left.equalTo(closeBtn.snp.bottom).offset(viewModel.closeBtnContentViewSpacing)
                    make.bottom.equalTo(-viewModel.contentInsetEdge.bottom)
                    make.right.equalTo(-viewModel.contentInsetEdge.right)
                }
            case .bottom:
                closeBtn.snp.makeConstraints { make in
                    make.left.equalTo(viewModel.contentInsetEdge.left)
                    make.bottom.equalTo(-viewModel.contentInsetEdge.bottom)
                    make.right.equalTo(-viewModel.contentInsetEdge.right)
                    make.height.equalTo(closeBtn.snp.width)
                }
                showView.snp.makeConstraints { make in
                    make.top.equalTo(viewModel.contentInsetEdge.top)
                    make.left.equalTo(viewModel.contentInsetEdge.left)
                    make.bottom.equalTo(closeBtn.snp.bottom).offset(-viewModel.closeBtnContentViewSpacing)
                    make.right.equalTo(-viewModel.contentInsetEdge.right)
                }
            case .right:
                closeBtn.snp.makeConstraints { make in
                    make.top.equalTo(viewModel.contentInsetEdge.top)
                    make.bottom.equalTo(-viewModel.contentInsetEdge.bottom)
                    make.right.equalTo(-viewModel.contentInsetEdge.right)
                    make.width.equalTo(closeBtn.snp.height)
                }
                showView.snp.makeConstraints { make in
                    make.top.equalTo(viewModel.contentInsetEdge.top)
                    make.left.equalTo(viewModel.contentInsetEdge.left)
                    make.bottom.equalTo(-viewModel.contentInsetEdge.bottom)
                    make.right.equalTo(closeBtn.snp.left).offset(-viewModel.closeBtnContentViewSpacing)
                }
            }
        }else{
            showView.snp.makeConstraints { make in
                make.top.equalTo(viewModel.contentInsetEdge.top)
                make.left.equalTo(viewModel.contentInsetEdge.left)
                make.bottom.equalTo(-viewModel.contentInsetEdge.bottom)
                make.right.equalTo(-viewModel.contentInsetEdge.right)
            }
        }

        arrowView.backgroundColor = viewModel.contentBackgroundColor
        arrowView.direction = viewModel.showDirection
        addSubview(arrowView)
        arrowView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: viewModel.arrowWidth, height: viewModel.arrowHeight))
            switch viewModel.showDirection {
            case .top:
                make.centerX.equalToSuperview().offset(viewModel.arrowOffset)
                make.top.equalTo(contentView.snp.bottom)
            case .left:
                make.centerY.equalToSuperview().offset(viewModel.arrowOffset)
                make.left.equalTo(contentView.snp.right)
            case .bottom:
                make.centerX.equalToSuperview().offset(viewModel.arrowOffset)
                make.bottom.equalTo(contentView.snp.top)
            case .right:
                make.centerY.equalToSuperview().offset(viewModel.arrowOffset)
                make.right.equalTo(contentView.snp.left)
            }
        }
    }

    func show() {
        guard
//            let window = YPApp.KeyWindow,
              let markView = viewModel.markView,
              let supView = viewModel.showInView else { return }
        supView.addSubview(self)
        // 确定展示框
//        let supFrame = supView.convert(supView.bounds, to: window)
//        let windowEdge = viewModel.bubbleShowWindowEdge
//        let showFrame = CGRect(x: supFrame.origin.x - windowEdge.left,
//                               y: supFrame.origin.y - windowEdge.top,
//                               width: supFrame.width - windowEdge.yp.horizontal,
//                               height: supFrame.height - windowEdge.yp.vertical)
        // 获取渲染大小

        self.snp.makeConstraints { make in
            let size: CGSize = .init(width: viewModel.bubbleSize.width, height: viewModel.bubbleSize.height + viewModel.arrowHeight)
            let spacing = viewModel.markViewSpacing
            let centerOffset = viewModel.markViewCenterOffset
            make.size.equalTo(size)
            switch viewModel.showDirection {
            case .top:
                make.centerX.equalTo(markView).offset(centerOffset)
                make.bottom.equalTo(markView.snp.top).offset(-spacing)
            case .left:
                make.centerY.equalTo(markView).offset(centerOffset)
                make.right.equalTo(markView.snp.left).offset(-spacing)
            case .bottom:
                make.centerX.equalTo(markView).offset(centerOffset)
                make.top.equalTo(markView.snp.bottom).offset(spacing)
            case .right:
                make.centerY.equalTo(markView).offset(centerOffset)
                make.left.equalTo(markView.snp.right).offset(spacing)
            }
        }
        Wmc_ViewSpringAnimation(self, isShow: true)
    }

    func dismiss() {
        Wmc_ViewSpringAnimation(self, isShow: false, block: {}, completion: { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.removeFromSuperview()
        })
    }
}
